package cz.anime.ppro.beans;

import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.anime.ppro.enums.UserRolesEnum;
import cz.anime.ppro.logic.ContentComponent;
import cz.anime.ppro.logic.FragmentComponent;
import cz.anime.ppro.logic.UserComponent;
import cz.anime.ppro.model.Content;
import cz.anime.ppro.model.Fragment;

@Component
@ManagedBean
@Scope("view")
public class FragmentBean {	
	
	private boolean admin = false;
	
	private String contentContent;
	private String contentType;
	
	private Fragment fragment;
	
	private List<Content> content;
	
	private int fragmentId;

	@Autowired
	private FragmentComponent fragmentComponent;
	
	@Autowired
	private ContentComponent contentComponent;
	
	@Autowired
	private UserComponent userComponent;
	
	@PostConstruct
	public void init(){
		
		HttpServletRequest req = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		Map<String,String[]> params = req.getParameterMap();
		
		if (params.containsKey("fragment")) {
			setFragmentId(Integer.parseInt(params.get("fragment")[0]));
			setFragment(fragmentComponent.getFragment(fragmentId));
			setContent(contentComponent.getContentByFragment(fragmentId));
		}
		
		if(userComponent.getUser(getCurrentUser()).getRole().equals(UserRolesEnum.ROLE_ADMIN.name())){
			admin = true;
		}
	}
	
	public void deleteContent(int contentId){
		
		Content deleteContent = contentComponent.getContent(contentId);
		
		contentComponent.delete(deleteContent);
		getContent().remove(deleteContent);		
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("/PPRO2/fragment.jsf?fragment="+fragmentId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void addNewContent(int fragId){
		Content newContent = new Content();
		newContent.setContent(contentContent);
		newContent.setType(contentType);
		newContent.setFragment(fragmentComponent.getFragment(fragId));
		newContent.setDate(Calendar.getInstance().getTime());
		
		contentComponent.save(newContent);
		
		try {
			FacesContext.getCurrentInstance().getExternalContext().redirect("/PPRO2/fragment.jsf?fragment="+fragId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public int getCurrentUser(){
		return userComponent.getUserByNick(userComponent.getCurrentUsernameFromSession()).getId();
	}

	public Fragment getFragment() {
		return fragment;
	}

	public void setFragment(Fragment fragment) {
		this.fragment = fragment;
	}

	public int getFragmentId() {
		return fragmentId;
	}

	public void setFragmentId(int fragmentId) {
		this.fragmentId = fragmentId;
	}

	public String redirect(){
		return "fragment";
	}

	public List<Content> getContent() {
		return content;
	}

	public void setContent(List<Content> content) {
		this.content = content;
	}

	public String getContentContent() {
		return contentContent;
	}

	public void setContentContent(String contentContent) {
		this.contentContent = contentContent;
	}

	public String getContentType() {
		return contentType;
	}

	public void setContentType(String contentType) {
		this.contentType = contentType;
	}

	public boolean isAdmin() {
		return admin;
	}

	public void setAdmin(boolean admin) {
		this.admin = admin;
	}
}
